package dq.study.concurrent.lock;

import java.util.Random;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * @author dongqun
 * @version 1.0
 * @description 类的功能描述
 * @date 2019/11/27 13:38
 */
public class ReentrantReadWriteLockTest {

    private static ReentrantLock lock = new ReentrantLock();
    private static ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
    private static ReentrantReadWriteLock.ReadLock readLock = readWriteLock.readLock();
    private static ReentrantReadWriteLock.WriteLock writeLock = readWriteLock.writeLock();
    private static Integer value = 0;

    private static void handleRead() {
        lock.lock();
        try {
            Thread.sleep(1000);
            System.out.println("读操作:" + value);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    private static void handleWrite(Integer value) {
        lock.lock();
        try {
            Thread.sleep(1000);
            System.out.println("写操作:" + value);
            ReentrantReadWriteLockTest.value = value;
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }


    private static void handleRead2() {
        try {
            readLock.lock();
            Thread.sleep(500);
            System.out.println("读操作:" + value);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            readLock.unlock();
        }
    }

    private static void handleWrite2(Integer value) {
        try {
            writeLock.lock();
            Thread.sleep(1000);
            System.out.println("写操作:" + value);
            ReentrantReadWriteLockTest.value = value;
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            writeLock.unlock();
        }
    }

    /**
     * @author dongqun
     * @description 用重入锁整个执行过程大约要20多秒，使用读写锁只需要4s
     * 由此可见在写比较少时，用读写锁可以大幅提升性能
     * @date 2019/11/27 14:25
     * @param
     * @return
     */
    public static void main(String[] args) {
        Thread thread1 = new TestReadThread();
        Thread thread2 = new TestWriteThread();
        new Thread(thread2).start();
        for (int i = 0; i < 18; i++) {
            new Thread(thread1).start();
        }
        new Thread(thread2).start();
    }


    private static class TestReadThread extends Thread {
        @Override
        public void run() {
//            ReentrantReadWriteLockTest.handleRead();//23s
            ReentrantReadWriteLockTest.handleRead2();//4s
        }
    }

    private static class TestWriteThread extends Thread {
        @Override
        public void run() {
//            ReentrantReadWriteLockTest.handleWrite(new Random().nextInt(100));
            ReentrantReadWriteLockTest.handleWrite2(new Random().nextInt(100));
        }
    }

}
